package com.cnswhy.cloud.server.comsumer.consumer.contribution;

import com.cnswhy.cloud.server.common.entity.mq.RegisterMsg;
import com.cnswhy.cloud.server.comsumer.event.RegisterMessageEvent;
import com.cnswhy.cloud.server.core.entity.Customer;
import com.cnswhy.cloud.server.core.entity.CustomerSplit;
import com.cnswhy.cloud.server.core.service.CustomerSplitService;
import com.cnswhy.cloud.server.core.service.customer.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 给所有上级用户增加荣耀值
 */
@Component
@Slf4j
public class ContributionAddValueConsumer implements RegisterMessageEvent {
    @Autowired
    CustomerService customerService;
    @Autowired
    CustomerSplitService customerSplitService;
    @Override
    public void register(RegisterMsg registerMsg) {
        log.info("给所有上级用户增加荣耀值:{} {}",registerMsg.getCustomerId(),registerMsg.getLoginName());
        Customer customer = customerService.getById(registerMsg.getCustomerId());
        while(customer.getPromoterId() != null){
            customer = customerService.getById(customer.getPromoterId());
            if(customer == null){
                break;
            }
            customer.setContributionValue(customer.getContributionValue() + 1);
            customerService.updateById(customer);
        }
        log.info("给所有上级用户增加荣耀值：完成");

    }
}
